package org.sardine.aop;

import java.lang.reflect.Method;
import java.util.List;

import org.aopalliance.aop.Advice;
import org.aopalliance.intercept.MethodInterceptor;
import org.apache.log4j.Logger;

import net.sf.cglib.proxy.MethodProxy;

public class CglibMethodInvocation extends ReflectiveMethodInvocation{
	
	private final Logger log = Logger.getLogger(CglibMethodInvocation.class);
	
	private final MethodProxy methodProxy;

	public CglibMethodInvocation(Object proxy, Method method, Object[] args, MethodProxy methodProxy, List<Advice> interceptors) {
		super(proxy, null, method, args, interceptors);
		this.methodProxy = methodProxy;
	}
	
	@Override
	public Object proceed() throws Throwable {
		
		if(this.currentInterceptorIndex == this.interceptors.size() - 1){
			return methodProxy.invokeSuper(proxy, args);
		}
		
		Object interceptor = this.interceptors.get(++currentInterceptorIndex);
		
		return ((MethodInterceptor)interceptor).invoke(this);
		
	}

}
